package ${allPackage}${controllerPackage};

import com.github.pagehelper.PageInfo;
import com.ant.common.utils.FastMap;
import com.ant.common.result.ResponseResult;
import com.ant.common.exceptions.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ${allPackage}${domainPackage}.${domainUpper};
import ${allPackage}${servicePackage}.${domainUpper}Service;

import java.util.List;
import java.util.Objects;

/**
 * ${domainDesc}控制层
 *
 * @description
 * @create ${createTime}
 */
@RestController
@RequestMapping(value = "/${domain}")
@Slf4j
@Api(tags = "${domainDesc}")
public class ${domainUpper}Controller {

    private final ${domainUpper}Service ${domain}Service;

    public ${domainUpper}Controller(${domainUpper}Service ${domain}Service) {
        this.${domain}Service = ${domain}Service;
    }

    /**
     * ${domainDesc}分页查询列表
     *
     * @param param 条件参数
     * @return ResponseResult
     */
    @PostMapping(value = "/${domain}Page", name = "${domainDesc}分页查询列表")
    @ApiOperation("${domainDesc}分页查询列表")
    public ResponseResult ${domain}Page(@RequestBody FastMap param){
        try {
            PageInfo<${domainUpper}> pages = ${domain}Service.getList(param.getInt("page"), param.getInt("limit"), param);
            log.info("${domainDesc}分页查询列表成功");
            return ResponseResult.success("${domainDesc}分页查询列表成功", pages.getList(), pages.getTotal());
        } catch (Exception e) {
            log.info("${domainDesc}分页查询列表失败：", e);
            return ResponseResult.fail("${domainDesc}分页查询列表失败：" + e.getMessage());
        }
    }

    /**
     * ${domainDesc}查询列表
     *
     * @param param 条件参数
     * @return ResponseResult
     */
    @PostMapping(value = "/allList", name = "${domainDesc}查询列表")
    @ApiOperation("${domainDesc}查询列表")
    public ResponseResult allList(@RequestBody FastMap param){
        try {
            List<${domainUpper}> list = ${domain}Service.getAllList(param);
            log.info("${domainDesc}查询列表成功");
            return ResponseResult.success("${domainDesc}查询列表成功", list);
        } catch (Exception e) {
            log.info("${domainDesc}查询列表失败：", e);
            return ResponseResult.fail("${domainDesc}查询列表失败：" + e.getMessage());
        }
    }

    /**
     * ${domainDesc}通过编号查询
     *
     * @param primaryKey
     * @return ResponseResult
     */
    @PostMapping(value = "/getById", name = "${domainDesc}通过编号查询")
    @ApiOperation("${domainDesc}通过编号查询")
    public ResponseResult getById(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到${domainDesc}编号");
            }

            ${domainUpper} ${domain} = ${domain}Service.getById(primaryKey);
            log.info("${domainDesc}通过编号查询成功");
            return ResponseResult.success("${domainDesc}通过编号查询成功", ${domain});
        } catch (Exception e) {
            log.info("${domainDesc}通过编号查询失败：", e);
            return ResponseResult.fail("${domainDesc}通过编号查询失败：" + e.getMessage());
        }
    }

    /**
     * 修改${domainDesc}（新增、修改）
     *
     * @param ${domain} ${domain}
     * @return ResponseResult
     */
    @PostMapping(value = "/edit", name = "修改${domainDesc}")
    @ApiOperation("修改${domainDesc}（新增、修改）")
    public ResponseResult edit(@RequestBody ${domainUpper} ${domain}){
        try {
            if (Objects.isNull(${domain}.get${primaryKey}()) || ${domain}.get${primaryKey}()${judgePrimaryKey}){
                //新增
                ${domain}Service.saveOne(${domain});
                log.info("${domainDesc}新增成功");
            }else {
                //编辑
                ${domain}Service.update(${domain});
                log.info("${domainDesc}修改成功");
            }
            return ResponseResult.success("${domainDesc}编辑成功");
        } catch (Exception e) {
            log.info("${domainDesc}编辑失败：", e);
            return ResponseResult.fail("${domainDesc}编辑失败：" + e.getMessage());
        }
    }

    /**
     * 根据id删除${domainDesc}
     *
     * @param primaryKey id
     * @return ResponseResult
     */
    @PostMapping(value = "/delete", name = "删除${domainDesc}")
    @ApiOperation("删除${domainDesc}")
    public ResponseResult delete(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到${domainDesc}编号");
            }
            ${domain}Service.delete(primaryKey);
            log.info("${domainDesc}删除成功");
            return ResponseResult.success("${domainDesc}删除成功");
        } catch (Exception e) {
            log.info("${domainDesc}删除失败：", e);
            return ResponseResult.fail("${domainDesc}删除失败：" + e.getMessage());
        }
    }
}
